package org.projectname.web.admin.book;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.hishenframework.core.data.jdbc.template.DataPaging;
import org.hishenframework.core.web.menu.MenuItem;
import org.hishenframework.core.web.message.Message;
import org.projectname.entity.book.Book;
import org.projectname.entity.book.BookPart;
import org.projectname.service.admin.book.BookPartService;
import org.projectname.service.admin.book.BookService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 图书分类控制器
 * @author didi
 *
 */
@Controller
@RequestMapping (BookController.PATH)
@MenuItem(type = "图书管理", label = "图书", order = 2)
public class BookController {
	
	public final static String PATH = "/admin/book/book";
	public final static String RELOAD = "redirect:" + PATH;
	
	@Resource
	private BookService bookService;

	@Resource
	private BookPartService bookPartService;
	
	//有该注解标记的会在所有请求时都触发，并自动装载至model中
	@ModelAttribute("bookParts")
	public List<BookPart> getBookParts() {
		return bookPartService.getAll();
	}
	
	/**
	 * json 测试
	 * @return
	 */
	@RequestMapping("/json")
	@ResponseBody  
	public Object jsonTest() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("1", "a");
		map.put("2", new Book());
		map.put("3", 3);
		return map;
	}
	
	/**
	 * 列表
	 * @param page
	 * @return
	 */
	@RequestMapping
	public String list(Model model,
			//该注解可以让dataPaging自动作为model的属性存入model
			@ModelAttribute DataPaging<Book> dataPaging,
			String bookname, Date from, Date to) {
		//设定分页大小默认20
//		dataPaging.setPageSize(10);
		bookService.search(dataPaging, bookname, from, to);
		return PATH + "/list";
	}
	
	/**
	 * 创建
	 * @return
	 */
	@RequestMapping("/create")
	public String create(Model model) {
//		model.addAttribute("bookParts", bookPartService.getAll()) ;
		return PATH + "/create";
	}

	/**
	 * 保存（限定提交方法只能为post方式）
	 * @param book
	 * @param result java代码验证
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public String save(Model model, @ModelAttribute @Valid Book book, BindingResult result, Message m) {
		if (result.hasErrors()) {
			return PATH + "/create";
		}
		m.setResultCount(bookService.save(book));
		return RELOAD;
	}
	
	/**
	 * 编辑
	 * @param id 图书类别主键
	 * @return
	 */
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable int id, Model model) {
		model.addAttribute("book", bookService.get(id));
		return PATH + "/edit";
	}
	
	/**
	 * 更新
	 * @param book
	 * @param result
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public String update(Model model, @ModelAttribute @Valid Book book, BindingResult result, Message m) {
		if (result.hasErrors()) {
			return PATH + "/edit";
		}	
		m.setResultCount(bookService.update(book));
		return RELOAD;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping("/delete/{id}")
	public String delete(@PathVariable int id, Message m) {
		m.setResultCount(bookService.delete(id));
		return RELOAD;
	}
	
}
